;;; +tex.el --- LaTeX -*- lexical-binding: t -*-

;;; Commentary:

;;; Code:

;; LaTeX displayed as WYSIWYG style
(use-package magic-latex-buffer
  :defer t
  :hook
  (latex-mode . magic-latex-buffer)
  :init
  (setq magic-latex-enable-block-highlight nil
	    magic-latex-enable-suscript        t
	    magic-latex-enable-pretty-symbols  t
	    magic-latex-enable-block-align     nil
	    magic-latex-enable-inline-image    nil
	    magic-latex-enable-minibuffer-echo nil))

;; -------- improve the performance of TeX --------
(use-package tex
  :ensure auctex
  :defer t
  :preface
  (defun cor5-TeX-remove-macro ()
    "Remove current \\macro{text} (unwrap) at point."
    (interactive)
    (when (TeX-current-macro)
      (let ((bounds (TeX-find-macro-boundaries))
	        (brace  (save-excursion
		              (goto-char (1- (TeX-find-macro-end)))
		              (TeX-find-opening-brace))))
	    (delete-region (1- (cdr bounds)) (cdr bounds))
	    (delete-region (car bounds) (1+ brace)))
      t))
  :config
  (add-hook 'LaTeX-mode-hook
	        (lambda ()
	          (turn-on-reftex)
	          (setq reftex-plug-into-AUCTeX t
		            TeX-show-compilation t
		            TeX-clean-confirm nil)
	          (outline-minor-mode) ;; use C-c @ to fold
	          ;; (hide-body) ;; fold when init
	          (local-set-key (kbd "M-S") #'cor5-TeX-remove-macro)))
  (setq-default TeX-engine 'xetex)
  :custom
  ;; (TeX-parse-self t) ;; conflict with magic-latex-buffer
  (TeX-master nil) ;; edit multi-file
  (when (executable-find "evince")
    (TeX-view-program-selection '((output-pdf "evince"))
			                    TeX-source-correlate-start-server t))
  ;; (TeX-view-program-list '(("pdf-tools" "TeX-pdf-tools-sync-view")))
  ;; (TeX-after-compilation-finished-functions #'TeX-revert-document-buffer)
  )

(use-package cdlatex
  :after tex
  :hook
  (latex-mode . cdlatex-mode)
  :config
  (turn-on-cdlatex)
  (cdlatex-command-alist
   '(;; ("blk" "insert block in beamer" "\\begin{block}{}
     ;;   ?
     ;; \\end{block}" cdlatex-position-cursor nil t nil)
     ;;        ("frm" "insert frame in beamer" "\\begin{frame}{}
     ;;   ?
     ;; \\end{frame}" cdlatex-position-cursor nil t nil)
     ("ind" "insert indent (new paragraph in beamer)" "\\setlength{\\parindent}{1.6em}" cdlatex-position-cursor nil t nil)
     ;; ("cod" "insert code" "\\begin{lstlisting}[language=]
     ;;   ?
     ;; \\end{lstlisting}" cdlatex-position-cursor nil t nil)
     ("Cod" "import code in file" "\\lstinputlisting[language=]{?}" cdlatex-position-cursor nil t nil)))
  (cdlatex-paired-parens "$([{"))

;; -------- defun & macrOs for LaTeX-mode --------

;; (fset 'HA-macro-TeX-indent
;;       (lambda (&optional arg)
;; 	"Keyboard macro."
;; 	(interactive "p")
;; 	(kmacro-exec-ring-item
;; 	 '("\\setlength{\\parindent{1.6em" 0 "%d")
;; 	 arg)))

;; (fset 'HA-macro-TeX-figure-args
;;       (lambda (&optional arg)
;; 	"Keyboard macro."
;; 	(interactive "p")
;; 	(kmacro-exec-ring-item
;; 	 (quote ("\\includegraphics[width=\\textwidth]{" 0 "%d"))
;; 	 arg)))

(provide '+tex)

;;; +tex.el ends here
